﻿using System.Collections.Generic;
using BBCode.Nodes;

namespace BBCode.Tags.BBCodeTags
{
    /// <summary>
    /// List BBCode Tag
    /// </summary>
    public class List : BBCodeComplex, ITag
    {
        private readonly ListItem _listItemTag;

        /// <summary>
        /// Initializes a new instance of the <see cref="List"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="templateName">Name of the template.</param>
        /// <param name="listItemTag">The list item tag.</param>
        /// <param name="canHaveChildren">if set to <c>true</c> [can have children].</param>
        /// <param name="priority">The priority.</param>
        public List(string name, string templateName, ListItem listItemTag, bool canHaveChildren = true, int priority = 0)
            : base(name, templateName, canHaveChildren, priority)
        {
            _listItemTag = listItemTag;
        }

        /// <summary>
        /// Gets the nodes.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <returns>The found tags.</returns>
        public new IEnumerable<INode> GetNodes(string text)
        {
            var results = new List<INode>();
            var nodes = base.GetNodes(text);

            foreach(Nodes.BBCodeComplex node in nodes)
            {
                results.Add(node);
                results.AddRange(_listItemTag.GetNodes(node.InnerText, node));    
            }

            return results;
        }
    }
}
